#include "DownloadMgr.h"

#include "DownloadThread.h"
#include <wx/wfstream.h>


#ifndef BUFFER_MAXLENGTH
#define BUFFER_MAXLENGTH 4096
#endif




DownloadMgr::DownloadMgr()
	{
	CurrentIndex = 0;
	DownloadState = 0;
	wxASSERT_MSG(1 == 0, wxT("DownloadMgr() should not be used..Always supply a URL!") );
	}

DownloadMgr::DownloadMgr(wxString url, wxString Id, wxWindow* parent)
	{
	this->SetWinParent(parent);
	this->AddUrl(url, Id);
	DownloadState = 0;
	CurrentIndex = 0;

	}

DownloadMgr::DownloadMgr(wxArrayString urls, wxArrayString Ids,wxWindow* parent)
	{
	wxASSERT_MSG(urls.GetCount() == Ids.GetCount(), wxT("Num of Urls doesnt match Num of Ids in DownloadMgr::DownloadMgr") );
	
	this->AddUrls(urls, Ids);
	this->SetWinParent(parent);
	DownloadState = 0;
	CurrentIndex = 0;
	}

DownloadMgr::~DownloadMgr()
	{
	}

void DownloadMgr::AddUrl(wxString url, wxString Id)
	{
	UrlQueue.Add(url);
	UrlIds.Add(Id);
	}

void DownloadMgr::AddUrls(wxArrayString urls, wxArrayString Ids)  //NOTE: might need to iron out indexing on this function
	{
	for(unsigned int i = 0; i <= (urls.GetCount()-1);i++)
		{
		UrlQueue.Add(urls.Item(i)); //will result in assert failure if accesses beyond what is there
		};

	for(unsigned int j = 0; j <= (Ids.GetCount()-1);j++)
		{
		UrlIds.Add(Ids.Item(j));
		};

	}

int DownloadMgr::GetQueueCount()
	{
	int returnNum;
	returnNum = static_cast<int> (UrlQueue.GetCount());
	return returnNum;
	}

int DownloadMgr::GetCurrentDownload()
	{
	return CurrentIndex;
	}

bool DownloadMgr::LastDownload()
	{
	if (CurrentIndex == UrlQueue.GetCount()-1)
		{
		return true;
		}
	else
		{
		return false;
		}
	}

bool DownloadMgr::ExistsInQueue(unsigned int index)
	{
	//first find if index is higher than # of items in array
	if(index > (UrlQueue.GetCount()-1) ){
		return false;
		}
	else 
		{
		if(UrlQueue.Item(index) == wxT(""))
			{
			return false;
			}
		else
			{
			return true;
			}
		}
	}

bool DownloadMgr::CheckQueueAndIdCount()
	{
	if(UrlQueue.GetCount() == UrlIds.GetCount())
		{
		return true;
		}
	else
		{
		return false;
		}
	}

wxString DownloadMgr::Download(int index)
	{
	DownloadState = true;
	//check if the index exists first
	wxASSERT_MSG(this->ExistsInQueue(index) == true,wxT("Index ") + wxString::Format(wxT("%d"),(int)index) + wxT(" was not found in the queue") );
	wxASSERT_MSG(this->CheckQueueAndIdCount() == true, wxT("UrlQueue and Id count did not match"));

	wxString s_Url = UrlQueue.Item(index);
	
	wxString ret_Filename = UrlIds.Item(index) + wxT(".mp4");


	


	DownloadThread* DnThread = new DownloadThread(d_Parent, s_Url,ret_Filename);
	
	
	
		
		DownloadState = false;
		CurrentIndex++;


		return ret_Filename;
	}

bool VerifyFLVFile(wxString filename)
	{
	
	char buf[3];

	std::ifstream VideoFile(filename.c_str(),std::ios::in | std::ios::binary);

	VideoFile.read(buf,3);
	if(!VideoFile)
		{
		wxMessageBox(wxT("Could not verify the video file because it did not open"));
		}
	VideoFile.close();
	//make sure the first 3 bytes are 0x46,0x4C,and 0x56 (meaning "F" "L" and "V")
	if(buf[1] == 0x46 && buf[2] == 0x4C && buf[3] == 0x56)
		{
		return true;
		}
	else
		{
		return false;
		}
	}

void DownloadMgr::SetWinParent(wxWindow* parent)
	{
	d_Parent = parent;
	}







